<?php

namespace App\Module\Sub;

use App\depository\PermissionsDep;
use App\depository\PlayerSecretDep;
use App\depository\RoleHasPermissionsDep;
use App\depository\RolesDep;
use App\depository\SubPermissionsDep;
use App\depository\TeamsDep;
use App\Enum\CommonEnum;
use App\Enum\PermissionsEnum;
use App\Enum\RolesEnum;
use App\Models\ManageSecretModel;
use App\Module\BaseModule;
use App\Service\DictService;
use App\depository\SubRolesDep;
use App\depository\SubRoleHasPermissionsDep;
use App\depository\ManageSecretDep;
use App\depository\SystemDep;

class SubSecretModule extends BaseModule
{

    public function init($request)
    {

        $param = $request->input();
        $teamId = $request->headers->get('TeamId');

        $dictService = new DictService();
        $dictService->setSecretGroup($teamId);


        $data['dict'] = $dictService->getDict();

        return self::response($data);

    }

    public function add($request)
    {
        $param = $request->input();

        $teamId = $request->headers->get('TeamId');

        for ($i = 0; $i < $param['number']; $i++) {

            $data['gh_id'] = $teamId;
            $data['secret'] = create_secret();
            $data['days'] = $param['days'];
            $data['group_name'] = $param['group_name'];
            $data['extra'] = $param['extra'];
            $data['status'] = ManageSecretDep::STATUS_OFF;

            $dep = new ManageSecretDep();
            $dep->add($data);
        }
        return self::response();
    }

    public function del($request)
    {
        $param = $request->input();

        $dep = new ManageSecretDep();

        $dep->del($param['id']);

        return self::response();
    }

    public function edit($request)
    {
        $param = $request->input();
        $teamId = $request->headers->get('TeamId');
        $param['gh_id'] = $teamId;
        $secretDep = new ManageSecretDep();
        $secretDep->edit($param);
        return self::response();
    }

    public function list($request)
    {
        $param = $request->input();
        $param['gh_id'] = $request->headers->get('TeamId');
        $mapDep = new ManageSecretDep();
        $resList = $mapDep->list($param);
        $list = $resList->getCollection()->toBase();
        $data['list'] = $list->map(function ($item) {
            return [
                'id' => $item->id,
                'secret' => $item->secret,
                'days' => $item->days,
                'steamid' => $item->steamid,
                'extra' => $item->extra,
                'status' => ManageSecretDep::STATUS[$item->status],
                'group_name' => $item->group_name,
                'created_at' => $item->created_at->toDateTimeString()
            ];
        });
        $data['page'] = [
            'per_page' => $param['per_page'],
            'current_page' => $param['current_page'],
            'total_page' => $resList->lastPage(),
            'total' => $resList->total(),
        ];
        return self::response($data);
    }

    public function active($request)
    {

        $param = $request->input();

        if (empty($param['secret'])) {
            return self::response([], '请输入密钥', 100);
        }

        if (empty($param['steamid'])) {
            return self::response([], '请输入steamid', 100);
        }

        $dep = new ManageSecretDep();
        $resSecret = $dep->firstBySecret($param['secret']);

        if (!$resSecret) {
            return self::response([], '请输入正确的密钥', 200);
        }

        if ($resSecret->status != ManageSecretDep::STATUS_OFF) {
            return self::response([], '密钥已被激活', 200);
        }

        $data = [
            'status' => ManageSecretDep::STATUS_ON,
            'steamid' => $param['steamid'],
        ];

        $dep->upd($resSecret->id, $data);

        $playerDep = new PlayerSecretDep();
        $resPlayer = $playerDep->firstBySteamId($resSecret->gh_id, $param['steamid']);
        if (!$resPlayer) {
            $data = [
                'team_id' => $resSecret->gh_id,
                'steamid' => $param['steamid'],
                'start_at' => date('Y-m-d H:i:s'),
                'end_at' => date('Y-m-d H:i:s', strtotime("+" . $resSecret->days . " days")),
                'group' => $resSecret->group_name,
            ];
            $playerDep->add($data);
        } else {
            $data = [
                'end_at' => date("Y-m-d H:i:s", strtotime("+{$resSecret->days} days", strtotime($resPlayer->end_at))),
                'group' => $resSecret->group_name,
            ];
            $playerDep->upd($resPlayer->id, $data);
        }

        return self::response();
    }

    public function query($request)
    {

        $param = $request->input();

        if (empty($param['steamid'])) {
            return self::response([], 'SteamId不能为空', 100);
        }

        $dep = new PlayerSecretDep();
        $res = $dep->getBySteamId($param['steamid']);
        if ($res->isEmpty()) {
            return self::response([], '所有工会未处于激活状态', 200);
        }

        $teamDep = new TeamsDep();

        $data['list'] = $res->map(function ($item) use ($teamDep) {
            $resTeam = $teamDep->first($item->team_id);
            return [
                'team' => $resTeam ? $resTeam->name : CommonEnum::DEFAULT_NULL,
                'start_at' => $item['start_at'],
                'end_at' => $item['end_at']
            ];
        });

        return self::response($data);
    }

    public function carousel($request)
    {
        $param = $request->input();
        $systemDep = new SystemDep();
        $res = $systemDep->first();
        $carousel = json_decode($res['carousel'], true);
        return self::response(['carousel' => $carousel]);
    }

}

